home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_10 / 9n10023a < prev    next >
Text File  |  1991-08-12  |  2KB  |  82 lines

  1.  
  2. /* --------------------------------------------------------------
  3.  
  4. FUNCTION REMOVE_NODE: The steps to remove a selected node are:
  5.  
  6. A.  Complain if list empty.
  7.  
  8. B.  Get a string from the user.
  9.  
  10. C.  If no such node, complain and get out.
  11.  
  12. D.  If there are duplicated decrement the count and get out.
  13.  
  14. E.  If this is the only node in the list, clear root and tail 
  15.     pointers.
  16.  
  17. F.  Else if it's the first node in the list adjust root pointer to new 
  18.     first node and new first node's bwd pointer.
  19.  
  20. G.  Else if it's the last node in the list adjust tail pointer to new 
  21.     last node and new last node's fwd pointer.
  22.  
  23. H.  Else it's in the middle of the list so adjust its predecessor and 
  24.     successor to point to each other.
  25.  
  26. I.  Free the node being removed along with its string and decrement
  27.     the number in use.
  28.  
  29. -------------------------------------------------------------- */
  30.  
  31. void remove_node(void)
  32. {
  33.         Node *ploc_node;        /* ptr to located node */
  34.         char string[21];        /* tmp holder for node's string */
  35.  
  36. /*A*/   if (proot_node == NULL) {
  37.                 printf("\n   List contains no nodes\n");
  38.                 return;
  39.         }
  40.  
  41. /*B*/   printf("\n   Enter string: ");
  42.         scanf("%20s", string);
  43.  
  44. /*C*/   ploc_node = locate_node(string, EXACT); 
  45.         if (ploc_node == NULL) {
  46.                 printf("No such node exists\n");
  47.                 return;
  48.         }
  49.  
  50. /*D*/   if (ploc_node->count > 1) {
  51.                 --ploc_node->count;
  52.                 printf("\n   One duplicate removed");
  53.                 return;
  54.         }
  55.  
  56.         if (ploc_node->pbwd == NULL) {
  57. /*E*/           if (ploc_node->pfwd == NULL) {
  58.                         proot_node = ptail_node = NULL;
  59.                 }
  60. /*F*/           else {
  61.                         proot_node = ploc_node->pfwd;
  62.                         ploc_node->pfwd->pbwd = NULL;
  63.                 }
  64.         }
  65.         else {
  66. /*G*/           if (ploc_node->pfwd == NULL) {
  67.                         ptail_node = ploc_node->pbwd;
  68.                         ploc_node->pbwd->pfwd = NULL;
  69.                 }
  70. /*H*/           else {
  71.                         ploc_node->pbwd->pfwd = ploc_node->pfwd;
  72.                         ploc_node->pfwd->pbwd = ploc_node->pbwd;
  73.                 }
  74.         }
  75.  
  76. /*I*/   free(ploc_node->pstring);
  77.         put_free_node(ploc_node);
  78.         --nodes_in_use;
  79.         printf("\n   Node removed");
  80. }
  81.  
  82.